import java.util.Arrays;

public class Demo2 {

    public static void main(String[] args) {
        Solution1 solution = new Solution1();
//        int[][] arr = new int[5][6];
//        System.out.println(arr[0].length);
//        System.out.println(arr.length);
        //  solution.minTimeToReach(arr);
        int[][] arr ={{0,0,0},{0,0,0}};
        System.out.println(arr.length);
        System.out.println(arr[0].length);
        System.out.println(solution.minTimeToReach(arr));
    }


}

class Solution1 {
    int endRow = 0;
    int endCol = 0;
    int count  =  0;
    int maxTime = 2147483647;
    public int minTimeToReach(int[][] moveTime) {

        endRow = moveTime.length - 1;
        endCol = moveTime[0].length-1;
        int i = 0;
        int j = 0;

        int spendTime = 0;
         repeat(moveTime, i, j,0);
         return maxTime;


    }

    public void repeat(int[][] moveTime, int i, int j,int spendTime) {

        if (i == endRow && j == endCol ) {
            if(spendTime < moveTime[i][j]){
                spendTime = moveTime[i][j]+1;
            }else{
                spendTime += 1;
            }

            if (maxTime > spendTime) {
                maxTime = spendTime;
            }
            return;
        }

            if(i == 0 && j == 0){

            }else{
                if(spendTime < moveTime[i][j]){
                    spendTime = moveTime[i][j] + 1  ;
                }else{
                    spendTime += 1;
                }
            }



        if (i < endRow) {

            repeat(moveTime, i + 1, j, spendTime);


        }
        if (j < endCol) {

            repeat(moveTime, i, j + 1, spendTime);

        }
        return;
    }
}